package com.game.utils;

import org.apache.commons.lang3.StringUtils;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class DateUtils {
    public static String FULLFORMAT = "yyyy-MM-dd HH:mm:ss";
    private static final Map<String, DateTimeFormatter> FORMATTER_CACHE = new ConcurrentHashMap<>();
    // Object类型转为Date
    public static Date transformDate(Object obj) {
        if (obj == null) {
            return null;
        }
        if (obj instanceof Date) {
            return (Date) obj;
        } else if (obj instanceof String) {
            return DateFormatCompatible((String) obj);
        } else if (obj instanceof Long) {
            return new Date((Long) obj);
        } else if (obj instanceof Instant) {
            return Date.from((Instant) obj);
        } else if (obj instanceof LocalDateTime) {
            return Date.from(((LocalDateTime) obj).atZone(ZoneId.systemDefault()).toInstant());
        } else {
            return DateFormatCompatible(obj.toString());
        }
    }
    public static Date parsePlus(String date) {
        return parsePlus(date, FULLFORMAT);
    }
    // String 类型转为 Date
    public static Date parsePlus(String date, String pattern) {
        if (StringUtils.isBlank(date)) {
            return null;
        }
        LocalDate localDate = LocalDate.parse(date, getFormatter(pattern));
        return Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
    }
    public static DateTimeFormatter getFormatter(String pattern) {
        return FORMATTER_CACHE.computeIfAbsent(pattern, DateTimeFormatter::ofPattern);
    }
    // 各种日期格式的转化
    public static Date DateFormatCompatible(String inputDate) {
        if (StringUtils.isBlank(inputDate)) {
            return null;
        }
        List<DateTimeFormatter> formatters = Arrays.asList(
                DateTimeFormatter.ofPattern("yyyy/M/d"),
                DateTimeFormatter.ofPattern("yyyy/M/d HH:mm:ss"),
                DateTimeFormatter.ofPattern("yyyy/M/d HH:mm"),

                DateTimeFormatter.ofPattern("yyyy-M-d"),
                DateTimeFormatter.ofPattern("yyyy-M-d HH:mm:ss"),
                DateTimeFormatter.ofPattern("yyyy-M-d HH:mm"),

                DateTimeFormatter.ofPattern("M/d/yyyy"),
                DateTimeFormatter.ofPattern("M/d/yyyy HH:mm:ss"),
                DateTimeFormatter.ofPattern("M/d/yyyy HH:mm"),

                DateTimeFormatter.ofPattern("M-d-yyyy"),
                DateTimeFormatter.ofPattern("M-d-yyyy HH:mm:ss"),
                DateTimeFormatter.ofPattern("M-d-yyyy HH:mm"),

                DateTimeFormatter.ofPattern("d-M-yyyy"),
                DateTimeFormatter.ofPattern("d-M-yyyy HH:mm:ss"),
                DateTimeFormatter.ofPattern("d-M-yyyy HH:mm"),

                DateTimeFormatter.ofPattern("d/M/yyyy"),
                DateTimeFormatter.ofPattern("d/M/yyyy HH:mm:ss"),
                DateTimeFormatter.ofPattern("d/M/yyyy HH:mm")
        );
        for (DateTimeFormatter formatter : formatters) {
            try {
                LocalDate localDate = LocalDate.parse(inputDate, formatter);
                return Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
            } catch (DateTimeParseException e) {
                // 忽略异常，继续尝试下一个格式
            }
        }
        return null;
    }
}
